package com.koo.modules.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.modules.product.dao.ProductBrandDao;
import com.koo.modules.product.entity.ProductBrandEntity;
import com.koo.modules.product.entity.vo.ImageVo;
import com.koo.modules.product.service.ProductBrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ProductBrandServiceImpl extends ServiceImpl<ProductBrandDao, ProductBrandEntity> implements ProductBrandService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<ProductBrandEntity> queryWrapper = new LambdaQueryWrapper<>();
        Object dataForm = params.get("dataForm");
        if(MyStrUtil.isNotEmpty(dataForm)){
            JSONObject jsonObject = JSON.parseObject(dataForm.toString());
            String key = jsonObject.getString("key");
            queryWrapper.like(MyStrUtil.isNotEmpty(key), ProductBrandEntity::getBrandName, key)
                    .or().like(MyStrUtil.isNotEmpty(key), ProductBrandEntity::getDescript, key);
        }
        IPage<ProductBrandEntity> page = this.page(
                new Query<ProductBrandEntity>().getPage(params),
                queryWrapper
        );
        page.getRecords().forEach(item -> {
            List<ImageVo> list = new ArrayList<>();
            if (MyStrUtil.isNotEmpty(item.getLogo())) {
                String[] logos = item.getLogo().split(",");
                for (String logo : logos ) {
                    ImageVo imageVo = new ImageVo();
                    imageVo.setUrl(logo);
                    list.add(imageVo);
                }
            }
            item.setPics(list);
        });
        return new PageUtils(page);
    }

    @Override
    public void update(ProductBrandEntity brand) {
        List<ImageVo> pics = brand.getPics();
        StringBuilder sb = new StringBuilder();
        if(CollectionUtils.isNotEmpty(pics)){
            pics.forEach(item -> {
                sb.append(item.getUrl()).append(",");
            });
        }
        brand.setLogo(sb.toString());
        this.updateById(brand);
    }

    @Override
    public ProductBrandEntity info(String brandId) {
        ProductBrandEntity brand = this.getById(brandId);
        List<ImageVo> list = new ArrayList<>();
        if (MyStrUtil.isNotEmpty(brand.getLogo())) {
            String[] logos = brand.getLogo().split(",");
            for (String logo : logos ) {
                ImageVo imageVo = new ImageVo();
                imageVo.setUrl(logo);
                list.add(imageVo);
            }
        }
        brand.setPics(list);
        return brand;
    }
}